package pl.poznan.put.kck;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class HTDLoader {
	public int fNameLength;
	public int dateLength;
	private int memoLength;
	private long OUT_ORGX;
	private long OUT_ORGY;
	private long OUT_EXTX;
	private long OUT_EXTY;
	private int numOfPackages;
	private int noLabels;
	public byte[] dateString;
	private byte[] memo;
	public byte[] fName;
	public PacketData[] data;
	public int minX;
	public int maxX;
	public int minY;
	public int maxY;
	public int pressMax;
	public int pressMin;
	public int[][] colorTable;

	public HTDLoader(String binFileName) {
		DataInputStream instr;
		createColorTable();
		try {
			instr = new DataInputStream(new FileInputStream(binFileName));
			fNameLength = Integer.reverseBytes(instr.readInt());
			fName = new byte[fNameLength];
			for (int i = 0; i < fNameLength; i++) {
				fName[i] = instr.readByte();
			}
			dateLength = Integer.reverseBytes(instr.readInt());
			dateString = new byte[dateLength];
			for (int i = 0; i < dateLength; i++) {
				dateString[i] = instr.readByte();
			}
			memoLength = Integer.reverseBytes(instr.readInt());
			memo = new byte[memoLength];
			for (int i = 0; i < memoLength; i++) {
				memo[i] = instr.readByte();
			}
			OUT_ORGX = Integer.reverseBytes(instr.readInt());
			OUT_ORGY = Integer.reverseBytes(instr.readInt());
			OUT_EXTX = Integer.reverseBytes(instr.readInt());
			OUT_EXTY = Integer.reverseBytes(instr.readInt());
			numOfPackages = Integer.reverseBytes(instr.readInt());
			
			data = new PacketData[numOfPackages];
			for (int i = 0; i < numOfPackages; i++) {
				data[i] = new PacketData(Integer.reverseBytes(instr.readInt()),
						Integer.reverseBytes(instr.readInt()), Integer
								.reverseBytes(instr.readInt()), Integer
								.reverseBytes(instr.readInt()), Integer
								.reverseBytes(instr.readInt()), Integer
								.reverseBytes(instr.readInt()));
			}
			noLabels = Integer.reverseBytes(instr.readInt());
			instr.close();
			
		} catch (IOException iox) {
			iox.printStackTrace();
		}
	}

	void createColorTable() {
		colorTable = new int[1025][3];
		int i;
		for(i=0;i<=255;i++)
		{
				colorTable[i][0]=255;
				colorTable[i][1]=i;
				colorTable[i][2]=0;
		}
		for(i=256;i<=2*256-1;i++)
		{
				colorTable[i][0]=(2*256-1)-i;
				colorTable[i][1]=255;
				colorTable[i][2]=0;
			}
		for(i=2*256;i<=3*256+1;i++)
		{
					colorTable[i][0]=0;
					colorTable[i][1]=255;
					colorTable[i][2]=i-2*256;
				}
		for(i=3*256;i<=4*256-1;i++)
		{
					colorTable[i][0]=0;
					colorTable[i][1]=(4*256-1)-i;
					colorTable[i][2]=255;
				}
	}
	void showPackages() {
		for (int i = 0; i < numOfPackages; i++)
			System.out.print(data[i].pkTime + ", " + data[i].pkX + ", "
					+ data[i].pkY + ", " + data[i].pkPressure + ", "
					+ data[i].pkAzimuth + ", " + data[i].pkAltitude + "\n");
	}
	void showInfo() {
		System.out.print("\nDane pliku: \nname length: " + fNameLength
				+ "\ndate length: " + dateLength + "\nmemo length: "
				+ memoLength + "\nnumber of packets: " + numOfPackages
				+ "\nOUT_ORGX: " + OUT_ORGX + "\nOUT_ORGY: " + OUT_ORGY
				+ "\nOUT_EXTX: " + OUT_EXTX + "\nOUT_EXTY: " + OUT_EXTY
				+ "\nno labels: "+ noLabels);
	}
	void findMinMax() {
		minX = data[0].pkX;
		maxX = data[0].pkX;
		minY = data[0].pkY;
		maxY = data[0].pkY;
		pressMax = data[0].pkPressure;
		for (int i = 1; i < data.length; i++) {
			if (data[i].pkX < minX)
				minX = data[i].pkX;
			else if (data[i].pkX > maxX)
				maxX = data[i].pkX;
			if (data[i].pkY < minY)
				minY = data[i].pkY;
			else if (data[i].pkY > maxY)
				maxY = data[i].pkY;
			if(data[i].pkPressure > pressMax) pressMax = data[i].pkPressure;
			else if(data[i].pkPressure < pressMin) pressMin = data[i].pkPressure;
		}
		System.out.print(pressMin + " " + pressMax + "\n\n\n");
	}
	int scaleX(int pos) {
		return (int) ((pos * 700)/ maxX);
	}
	int scaleY(int pos) {
		return (int) ((pos * 700)/ maxY);
	}
	public int getColIndex(int press) {
		return (int) ((press * 1025)/pressMax);
	}
}

class PacketData {
	int pkTime;
	int pkX;
	int pkY;
	int pkPressure;
	int pkAzimuth;
	int pkAltitude;

	public PacketData(int time, int x, int y, int press, int azi, int alti) {
		pkTime = time;
		pkX = x;
		pkY = y;
		pkPressure = press;
		pkAzimuth = azi;
		pkAltitude = alti;
	}
}